package com.dao.impl;

import java.util.List;

import com.base.BaseDao;
import com.dao.TopicDao;
import com.entity.Topic;
import com.exception.DaoException;

public class TopicDaoImpl extends BaseDao<Topic> implements TopicDao {

	@Override
	public List<Topic> selectTopics() throws DaoException {
		return executeQuery("select * from topic");
	}

	@Override
	public boolean addTopic(String tname) throws DaoException {
		return executeUpdate("INSERT INTO `news_db`.`topic` (`tname`) VALUES (?)", tname) > 0;
	}

	@Override
	public boolean deleteTopic(Integer tid) throws DaoException {
		return executeUpdate("delete from topic where tid = ?", tid) > 0;
	}

	@Override
	public Topic getTopic(Integer tid) throws DaoException {
		List<Topic> topics = executeQuery("select * from topic where tid = ?", tid);
		if (topics.size() == 0) {
			return null;
		}
		return topics.get(0);
	}

	@Override
	public boolean updateTopic(Integer tid, String tname) throws DaoException {
		return executeUpdate("UPDATE `news_db`.`topic` SET `tname`=? WHERE (`tid`=?)", tname,tid) > 0;
	}

	@Override
	public List<Topic> selectTopicsByPage(int page, int row) throws DaoException {
		return executeQuery("select * from topic limit ?,?", (page - 1) * row, row);
	}

}
